/*
 * This file is part of ksPlatform
 * 
 * Copyright (c) 2014-2015
 * 
 * This file is dual-licensed under the AGPLv3 (http://www.gnu.org/licenses/agpl-3.0.html) 
 * 
 * If you are unsure which license is appropriate for your use, please contact the sales department
 * at 97041437@qq.com.
 */
package cn.com.ksplatform.ui.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import cn.com.ksplatform.ui.servlet.VelocityServlet;

@Controller
@RequestMapping("/modules/ui/velocity")
public class VelocityController {
	
	@RequestMapping("loadPage")
	public String loadPage(HttpServletRequest request,HttpServletResponse response){
		String vm = request.getAttribute(VelocityServlet.REQUEST_URL_REQATTR).toString();
		vm=vm.substring(request.getContextPath().length()+1);
		if(vm.endsWith("_script.vm")){
			response.setContentType("text/javascript;charset=utf-8");
			java.util.Date date = new java.util.Date();    
			response.setDateHeader("Last-Modified",date.getTime()); //Last-Modified:页面的最后生成时间 
			response.setDateHeader("Expires",date.getTime()+604800); //Expires:过时期限值 
			response.setHeader("Cache-Control", "max-age=604800, must-revalidate"); //Cache-Control来控制页面的缓存与否,public:浏览器和缓存服务器都可以缓存页面信息；
			response.setHeader("Pragma", "Pragma"); //Pragma:设置页面是否缓存，为Pragma则缓存，no-cache则不缓存
		}
		int sessionIdx = vm.lastIndexOf(".vm;jsessionid");
		if(sessionIdx != -1){
			return vm.substring(0, sessionIdx);
		}
		return vm.substring(0,vm.length()-3);
	}
	
}
